package cn.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * BIO基本用法：
 *      文件io：常用的流
 *      网络io：，指的是 java中的网络编程
 *
 * @author Mxb
 * @version 1.0
 * @date 2020/6/28 9:53
 */
public class TCPServer {
    public static void main(String[] args) throws IOException {
        //1.创建 ServerSocket 对象
        ServerSocket ss = new ServerSocket(9999);

        while (true) {
            //2.监听客户端
            Socket socket = ss.accept(); //阻塞

            //3.从连接中取出输入流来接收消息
            InputStream is = socket.getInputStream(); //阻塞

            byte[] b = new byte[10];

            is.read(b);
            String clientIP = socket.getInetAddress().getHostAddress();

            System.out.println(clientIP + "说：" + new String(b).trim());

            //4.从连接中取出输出流并
            OutputStream os = socket.getOutputStream();

            os.write("没钱".getBytes());
            //关闭
            socket.close();

        }
    }
}
